/*
 * @Author: xzy 1453077527@qq.com
 * @Date: 2025-08-24 00:34:58
 * @LastEditors: xzy 1453077527@qq.com
 * @LastEditTime: 2025-08-26 17:05:29
 * @Description:
 */
package com.xtedu.examproject.dao.impl;

import com.xtedu.examproject.dao.base.TeacherCourseDao;
import com.xtedu.examproject.entity.TeacherCourse;
import com.xtedu.examproject.util.DBUtil;
import com.xtedu.examproject.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class TeacherCourseDaoImpl implements TeacherCourseDao {
    @Override
    public int insert(TeacherCourse teacherCourse) throws Exception {
        String sql = "insert into teachers_courses (teacher_id, course_id,state) values (?, ?,?)";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, teacherCourse.getTeacherId());
            ps.setInt(2, teacherCourse.getCourseId());
            ps.setBoolean(3,teacherCourse.isState());
            return ps.executeUpdate();
        }
    }

    @Override
    public int update(TeacherCourse teacherCourse) throws Exception {
        String sql = "update teachers_courses set state=? where teacher_id=? and course_id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setBoolean(1, teacherCourse.isState());
            ps.setInt(2, teacherCourse.getTeacherId());
            ps.setInt(3, teacherCourse.getCourseId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int delete(TeacherCourse teacherCourse) throws Exception {
        String sql = "delete from teachers_courses where teacher_id=? and course_id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, teacherCourse.getTeacherId());
            ps.setInt(2, teacherCourse.getCourseId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int deleteById(int id) throws Exception {
        return 0;
    }

    @Override
    public TeacherCourse findById(int id) throws Exception {
        String sql = "select * from teachers_courses where teacher_id=?";
        try(Connection conn = DBUtil.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            TeacherCourse teacherCourse = new TeacherCourse();
            while (rs.next()) {
                teacherCourse.setTeacherId(id);
                teacherCourse.setTeacherId(rs.getInt("teacher_id"));
                teacherCourse.setCourseId(rs.getInt("course_id"));
                teacherCourse.setState(rs.getBoolean("state"));
            }
            return teacherCourse;
        }
    }

    @Override
    public List<TeacherCourse> findAll() throws Exception {
        String sql = "select * from teachers_courses";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ResultSet rs = ps.executeQuery();
            List<TeacherCourse> teacherCourses = new ArrayList<TeacherCourse>();
            while (rs.next()) {
                TeacherCourse teacherCourse = new TeacherCourse();
                teacherCourse.setTeacherId(rs.getInt("teacher_id"));
                teacherCourse.setCourseId(rs.getInt("course_id"));
                teacherCourse.setState(rs.getBoolean("state"));
                teacherCourses.add(teacherCourse);
            }
            return teacherCourses;
        }
    }

    @Override
    public PageHelper findAllByPage(int pageindex, int pagesize) throws Exception {
        return null;
    }

    @Override
    public PageHelper findPageByRoleIds(int pageindex, int pagesize, int... roleIds) throws Exception {
        return null;
    }

    @Override
    public TeacherCourse findByTeacherIdAndCourseId(int teacherId,int courseId) throws Exception {
        String sql = "select * from teachers_courses where teacher_id=? and course_id=?";
        try(Connection conn = DBUtil.getConnection();
            PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, teacherId);
            ps.setInt(2, courseId);
            ResultSet rs = ps.executeQuery();
            TeacherCourse teacherCourse = new TeacherCourse();
            while (rs.next()) {
                teacherCourse.setTeacherId(rs.getInt("teacher_id"));
                teacherCourse.setCourseId(rs.getInt("course_id"));
                teacherCourse.setState(rs.getBoolean("state"));
            }
            return teacherCourse;
        }
    }
}
